﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Net.Mime;
using ToppingControl.Models;

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Diagnostics;
using System.Drawing.Printing;

namespace ToppingControl.Controllers
{
    public class PDF_PRINTController : Controller
    {
      
        //
        // GET: /PDF_PRINT/
       public class PdfResult : ActionResult
       {

         List< TopCordActModel.TopCordAct_DS> _Input;
         string _prod;
         string _frRow;
         string _ToRow;
          BaseFont THCodefont = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\CORDIA.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
          BaseFont EnCodefont = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\tahoma.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
          PdfContentByte pcb;

          public PdfResult(List<TopCordActModel.TopCordAct_DS> data,string prod="B",string fr ="",string to="")
          {
             _Input = data;
             _prod = prod;
             _frRow = fr;
             _ToRow = to;
          }

          public override void ExecuteResult(ControllerContext context)
          {          

             try
             {
                if (_Input.Count <= 0)
                {
                   return;
                }
                var response = context.HttpContext.Response;
                response.ContentType = "application/pdf";
                //var cd = new ContentDisposition
                //{
                //   Inline = true,
                //   FileName = "Before.pdf",
                //};
                //response.AddHeader("Content-Disposition", cd.ToString());

                //Document doc = new Document(PageSize.A4.Rotate());
                Document doc = new Document(PageSize.A4.Rotate(),0,0,20,0);
                PdfWriter writer = PdfWriter.GetInstance(doc, context.HttpContext.Response.OutputStream);
              
                PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
                //writer.AddJavaScript("this.print(true);",false );
                writer.SetOpenAction(action);

                doc.Open();
                pcb = writer.DirectContentUnder;
           
                //printCodeTest(doc);
                if (_prod == "A")
                   PrintAfter(doc);
                else
                   PrintBefore(doc);
           
                doc.Close();             
         

                   
             }
             catch (Exception ex)
             {
                WinNemoLib.Nemo.Error(ex);               
             }
        
          }

            private void PrintTextCentered(string text, int x, int y) {
              pcb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text, x, y, 0);
          }
            private void PrintText(string text, int x, int y,int z=0) {
              pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, z);
          }
          public void printCodeTest(Document doc)
          {
             doc.SetMargins(50, 50, 25, 25);
            Font Nfont = new Font(THCodefont, 60, Font.BOLD);
            Image image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode("HN-43MH+121204R011+500+INOACINTERNATIONAL+2013-05-09+001", 5, 5));    
            image.ScaleToFit(70, 70);
            doc.Add(image);
            doc.Add(new Phrase(" "));
            //doc.Add(new Phrase("8858722100049", Nfont)); 


            image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode("HN-43MH+121204R011+500+INOACINTERNATIONAL+2015-05-09+001", 5, 5));
            image.ScaleToFit(70, 70);
            doc.Add(image);
          //  doc.Add(new Phrase("1000568858722", Nfont));
               doc.Add(new Phrase(" "));

               image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode("HNS-38+121204R011+500+INOACINTERNATIONAL+2013-05-09+001", 5, 5));
            image.ScaleToFit(70, 70);
            doc.Add(image);
            doc.Add(new Phrase(" "));
           // doc.Add(new Phrase("HNS38+121204R011+500+INOACINTERNATIONAL+2015-05-09+001", Nfont));
             

            image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode("HNS-38+121204R011+500+INOACINTERNATIONAL+2013-05-09+001", 5, 5));
          //  doc.Add(new Phrase("HNS38+121204R011+500+INOACINTERNATIONAL+2013-05-09+001", Nfont));
           

         //   image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode("PS-651-A+130313R003+897+20130328", 5, 5));
         //   image.ScaleToFit(70, 70);
         //   doc.Add(image);  doc.Add(new Phrase(" "));
         ////   doc.Add(new Phrase("PS-651-A+130313R003+897+20130328", Nfont));
          
         //   image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode("PS-651-A+130313R003+897+20150328", 5, 5));
         //   image.ScaleToFit(70, 70);
         //   doc.Add(image);  doc.Add(new Phrase(" "));
        //    doc.Add(new Phrase("PS-651-A+130313R003+897+20150328", Nfont));
             

          }
             private void PrintXAxis(int y) {
               pcb.SetFontAndSize(EnCodefont, 7);
              int x = 900;
              while(x>=0) {
                  if (x%20==0) {
                      PrintTextCentered(""+x, x, y+8);
                      PrintTextCentered("|", x, y);
                  }
                  else {
                      PrintTextCentered(".",x,y);
                  }
                  x -= 5;
              }
          }

             private void PrintYAxis(int x) {
                 pcb.SetFontAndSize(EnCodefont, 7);
              int y = 600;
              while(y>=0) {
                  if (y%20==0) {
                      PrintText("__ "+y, x, y);
                  }
                  else {
                      PrintText("_", x, y);
                  }
                  y = y - 5;
              }
          }

          public void PrintBefore(Document doc)
          {
             Font Nfont = new Font(THCodefont, 60, Font.BOLD);
             Font Bfont = new Font(EnCodefont, 75, Font.BOLD);
             Font Hfont = new Font(THCodefont, 65, Font.BOLD);
             
             TopCordActModel.TopCordAct_DS data;
             for (int i = Convert.ToInt16(_frRow) ; i <= Convert.ToInt16(_ToRow); i++)
             {
                data = _Input[0];
                if (i > 0)
                {
                   doc.NewPage();
                }
                pcb.BeginText();
                //PrintXAxis(580);
                //PrintXAxis(50);

                //PrintYAxis(40);
                //PrintYAxis(750);
                
                pcb.SetFontAndSize(EnCodefont, 16);
                 //code left  

                PrintText("B", 90, 540,-90);
                PrintText("B", 90, 390,-90);
                PrintText("B", 90, 230,-90);
                PrintText("B", 90, 75,-90);

                PrintText(data._Topcrdcd, 30, 570,-90);
                PrintText(data._Topcrdcd, 30, 415, -90);
                PrintText(data._Topcrdcd, 30, 265, -90);
                PrintText(data._Topcrdcd, 30, 100, -90);

                //code right  
                PrintText("B", 752, 532,90);
                PrintText("B", 752, 380,90);
                PrintText("B", 752, 225,90);
                PrintText("B", 752, 65,90);
                PrintText(data._Topcrdcd, 820, 500,90);
                PrintText(data._Topcrdcd, 820, 345, 90);
                PrintText(data._Topcrdcd, 820, 190, 90);
                PrintText(data._Topcrdcd, 820, 35, 90);

                pcb.EndText();
              
                PdfPCell header = new PdfPCell(new Phrase("ใบรายงานผลผลิตส่วนฉาบผ้าใบ", Nfont));
                header.HorizontalAlignment = 1;
                //header.MinimumHeight = 90f;
                header.Border = 0;
             //   //header.Colspan = 2;

             //   // A or B
                PdfPTable tableH = new PdfPTable(1);
                float[] widthH = new float[] { 4f };
                tableH.SetWidths(widthH);
                tableH.DefaultCell.Border = 0;
                tableH.WidthPercentage = 100;
                tableH.DefaultCell.PaddingBottom = 1f;
                header.PaddingBottom = 1f;
                tableH.AddCell(header);
                tableH.DefaultCell.HorizontalAlignment = 1;
                tableH.AddCell(new Phrase(data._Topcrdcd,Nfont));

                PdfPTable table = new PdfPTable(2);
                table.WidthPercentage = 100;
                table.DefaultCell.Border = 0;
                table.DefaultCell.HorizontalAlignment = 2;

                string code = data._Topcrdcd + "+" + data._Tcrdlot + "+" + i.ToString() + "+" + data._Prddate + "+" + data._Expired + "+" + data._Manufc + "+" + _prod;
                
                 Image image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode(code, 5, 5));
                image.ScaleToFit(35, 35);

       
                image.SetAbsolutePosition(765, 520);
                doc.Add(image);

                image.SetAbsolutePosition(765, 365);
                doc.Add(image);

                image.SetAbsolutePosition(765, 210);
                doc.Add(image);

                image.SetAbsolutePosition(765, 55);
                doc.Add(image);

                float[] widths = new float[] {  2f, 2f };
                table.SetWidths(widths);
              
                //PdfPCell middle = new PdfPCell(new Paragraph("", Bfont));
                //middle.Border = 0;
                //middle.VerticalAlignment = 1;
                //middle.Rowspan = 6;
                //middle.Rotation = -90;
                //middle.HorizontalAlignment = 1;
                //middle.MinimumHeight = 450f;
          
                ////code left
                //table.AddCell(middle);

                //header.HorizontalAlignment = 1;
                //PdfPCell tem = new PdfPCell(new Phrase("", Nfont));
                //tem.Border = 0;
                //tem.Colspan = 2;
                //tem.MinimumHeight = 50f;
                //table.AddCell(tem);
                //middle.HorizontalAlignment = 1;
                //middle.Rotation = 90;
                 //code right
               // table.AddCell(middle);

                PdfPCell desc;
                desc = new PdfPCell(new Paragraph(data._Tcrdlot, Nfont));
                desc.Border = 0;
                desc.HorizontalAlignment = 0;
                table.AddCell(new Phrase("ล็อตนัมเบอร์ : ", Nfont));
                table.AddCell(desc);
                desc = new PdfPCell(new Paragraph((i).ToString(), Nfont));
                desc.Border = 0;
                table.AddCell(new Phrase("ม้วนที่ : ", Nfont));
                table.AddCell(desc);
                desc = new PdfPCell(new Paragraph(data._Prddate, Nfont));
                desc.Border = 0;
                table.AddCell(new Phrase("วันที่ผลิต : ", Nfont));
                table.AddCell(desc);
                desc = new PdfPCell(new Paragraph(data._Expired, Nfont));
                desc.Border = 0;
                table.AddCell(new Phrase("วันที่หมดอายุ : ", Nfont));
                table.AddCell(desc);
                desc = new PdfPCell(new Paragraph(data._Manufc, Nfont));
                desc.Border = 0;
                table.AddCell(new Phrase("ผู้ผลิต : ", Nfont));
                table.AddCell(desc);


                image.SetAbsolutePosition(50, 520);
                doc.Add(image);

                image.SetAbsolutePosition(50, 365);
                doc.Add(image);

                image.SetAbsolutePosition(50, 210);
                doc.Add(image);

                image.SetAbsolutePosition(50, 55);
                doc.Add(image);


                doc.Add(tableH);
                doc.Add(table);
         
             }
          }

          public void PrintAfter(Document doc)
          {
             Font Nfont = new Font(THCodefont, 45, Font.BOLD);
             Font Bfont = new Font(EnCodefont, 50, Font.BOLD);
             Font Hfont = new Font(THCodefont, 55, Font.BOLD);

             TopCordActModel.TopCordAct_DS data;
             for (int i = 0; i < _Input.Count; i++)
             {
                data = _Input[i];
                if (i > 0)
                {
                   doc.NewPage();
                }
                

                PdfPCell header = new PdfPCell(new Phrase("ใบรายงานผลผลิตส่วนฉาบผ้าใบ", Nfont));
                header.HorizontalAlignment = 1;
                header.MinimumHeight = 90f;
                header.Border = 0;
                //header.Colspan = 2;

                // A or B
                PdfPTable tableH = new PdfPTable(2);
                float[] widthH = new float[] { 4f, 1f };
                tableH.SetWidths(widthH);
                tableH.DefaultCell.Border = 0;
                tableH.WidthPercentage = 100;
                tableH.AddCell("");
                tableH.AddCell("");
                
                tableH.AddCell(header);
                header.MinimumHeight = 100;
                tableH.DefaultCell.HorizontalAlignment = 0;
                tableH.DefaultCell.VerticalAlignment = 1;
                tableH.DefaultCell.MinimumHeight = 100;
                
                tableH.AddCell(new Phrase(_prod, Bfont));

                PdfPTable tableT = new PdfPTable(3);
                tableT.WidthPercentage = 100;
                tableT.DefaultCell.Border = 0;
                tableT.DefaultCell.HorizontalAlignment = 2;
                string code = data._Topcrdcd + "+" + data._Tcrdlot + "+" + data._Rollno + "+" + data._Prddate + "+" + data._Expired + "+" + data._Manufc + "+" + data._Lenght + "+" + _prod;
                Image image = iTextSharp.text.Image.GetInstance(QRCodeHtmlHelper.enCode_barcode(code, 5, 5));

                image.ScaleToFit(35, 35);                            
                image.SetAbsolutePosition(150,450 );
                doc.Add(image);
                image.SetAbsolutePosition(720, 350);
                doc.Add(image);
                image.SetAbsolutePosition(720, 30);
                doc.Add(image);

                float[] widths = new float[] {1f, 1f,2f };
                tableT.SetWidths(widths);
                tableT.DefaultCell.HorizontalAlignment = 1;
                tableT.AddCell("");
                PdfPCell tem = new PdfPCell(new Phrase("ชนิดผ้าใบ", Nfont));
                tem.Border = 0;           
                tem.MinimumHeight = 100f;
                tem.HorizontalAlignment = 1;
                tableT.AddCell(tem);

                PdfPCell middle = new PdfPCell(new Paragraph(data._Topcrdcd, Bfont));
                middle.Border = 0;
                middle.MinimumHeight = 100f;
                tableT.AddCell(middle);

                PdfPTable table = new PdfPTable(4);
                float[] wid = new float[] { 2f, 2f, 1.5f, 1.5f };
                table.SetWidths(wid);
                table.WidthPercentage = 95;
                table.DefaultCell.Border = 0;
                table.DefaultCell.HorizontalAlignment = 2;
                table.DefaultCell.VerticalAlignment = 1;
                PdfPCell desc;
                desc = new PdfPCell(new Paragraph(data._Tcrdlot, Nfont));
                desc.Border = 0;
                desc.HorizontalAlignment = 0;
                table.AddCell(new Phrase("ล็อตนัมเบอร์ : ", Nfont));
                table.AddCell(desc);
                desc = new PdfPCell(new Paragraph(data._Rollno, Nfont));
                desc.Border = 0;
                table.AddCell("");
                table.AddCell("");
                /*** R2 ***/
                table.AddCell(new Phrase("ม้วนที่ : ", Nfont));
                table.AddCell(desc);

                table.AddCell(new Phrase("ความยาว : ", Nfont));
                PdfPCell widthCell = new PdfPCell(new Paragraph(data._Lenght, Nfont));
                widthCell.Rowspan = 2;
                widthCell.Colspan = 1;
                widthCell.HorizontalAlignment = 1;
                widthCell.VerticalAlignment = 1;
                
                table.AddCell(widthCell);
                
                /*** R3 ***/

                desc.Border =0;
                table.AddCell(new Phrase("วันที่ผลิต : ", Nfont));
                desc = new PdfPCell(new Paragraph(data._Prddate, Nfont));
                desc.Border = 0;
                table.AddCell(desc);               
                table.AddCell("");
                /*** R4 ***/

                desc = new PdfPCell(new Paragraph(data._Expired, Nfont));
                desc.Border = 0;
                table.AddCell(new Phrase("วันที่หมดอายุ : ", Nfont));
                table.AddCell(desc);
                table.AddCell("");
                PdfPCell nmCell = new PdfPCell(new Paragraph(data._Empname, Nfont));
                nmCell.Rowspan = 2;
                nmCell.HorizontalAlignment = 1;
                nmCell.VerticalAlignment = 1;
                table.AddCell(nmCell);
                
                /*** R5 ***/
                desc = new PdfPCell(new Paragraph(data._Manufc, Nfont));
                desc.Border = 0;
                table.AddCell(new Phrase("ผู้ผลิต : ", Nfont));
                table.AddCell(desc);
                table.AddCell(new Phrase("ชื่อพนักงาน : ", Nfont));  
                desc = new PdfPCell(new Paragraph(data._Empname, Nfont));
                table.AddCell(desc);
                

                doc.Add(tableH);
                doc.Add(tableT);
                doc.Add(table);
             }
          }
       }

    

        public ActionResult Index(string topcode , string cust ,string prddt, string tirelot,string rFrm , string rTo)
        {

           List<TopCordActModel.TopCordAct_DS> list = new List<TopCordActModel.TopCordAct_DS>();
           TopCordActModel mdSearch = new TopCordActModel();
           list = mdSearch.LIST_FOR_PRINT(topcode, cust, tirelot, "", "", prddt);
           
           return new PdfResult(list,"B",rFrm,rTo);
        }

        public ActionResult After(string topcode, string cust, string prddt, string tirelot, string rollno)
        {
           List<TopCordActModel.TopCordAct_DS> list = new List<TopCordActModel.TopCordAct_DS>();
           TopCordActModel mdSearch = new TopCordActModel();
           list = mdSearch.LIST_ACT_FORTOP(topcode, cust, tirelot, rollno, "", prddt);
           return new PdfResult(list,"A");
        }

    }
}
